package com.haitaoit.chengquan.module.center_service.adapter.xuqiu_order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center_service.model.ServiceXuqiuModel;
import com.haitaoit.chengquan.view.MyListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */
public class ServiceXuqiuAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<ServiceXuqiuModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public ServiceXuqiuAdapter(Context context) {
        this.context = context;
    }

    public List<ServiceXuqiuModel> getList() {
        return list;
    }

    public void setList(List<ServiceXuqiuModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.service_xuqiu_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ser_xuqiu_itemll:
                    case R.id.item_ll:
                        ServiceXuqiuAdapter.this.backCall.deal(R.id.ser_xuqiu_itemll, position);
                        break;
                    case R.id.check_baojia:
                        ServiceXuqiuAdapter.this.backCall.deal(R.id.check_baojia, position);
                        break;
                    case R.id.service_finish:
                        ServiceXuqiuAdapter.this.backCall.deal(R.id.service_finish, position);
                        break;
                    case R.id.go_eva:
                        ServiceXuqiuAdapter.this.backCall.deal(R.id.go_eva, position);
                        break;
                    case R.id.check_eva:
                        ServiceXuqiuAdapter.this.backCall.deal(R.id.check_eva, position);
                        break;
                }
            }
        };
        holder.ser_xuqiu_itemll.setOnClickListener(listener);
        holder.item_ll.setOnClickListener(listener);
        holder.check_baojia.setOnClickListener(listener);
        holder.service_finish.setOnClickListener(listener);
        holder.go_eva.setOnClickListener(listener);
        holder.check_eva.setOnClickListener(listener);

        holder.check_baojia.setVisibility(View.GONE);// 查看报价
        holder.service_finish.setVisibility(View.GONE);// 已服务
        holder.go_eva.setVisibility(View.GONE);// 评价需求商
        holder.check_eva.setVisibility(View.GONE);// 查看评价
        holder.xuqiu_itemll.setVisibility(View.VISIBLE);

        // 订单状态(1报价成功显示查看报价2报价失败显示查看报价3已支付显示已完成4已完成)
        if (list.get(position).getOrder_status().equals("1")) {
            holder.ser_xuqiu_state.setText("已报价");
            holder.check_baojia.setVisibility(View.VISIBLE);
        } else if (list.get(position).getOrder_status().equals("2")) {
            holder.ser_xuqiu_state.setText("报价失败");
            holder.check_baojia.setVisibility(View.VISIBLE);
        } else if (list.get(position).getOrder_status().equals("3")) {
            holder.ser_xuqiu_state.setText("已支付");
            // 是否服务1否2是
            if (list.get(position).getIs_service().equals("1")) {
                holder.xuqiu_itemll.setVisibility(View.VISIBLE);
                holder.service_finish.setVisibility(View.VISIBLE);// 已服务
            } else {
                holder.xuqiu_itemll.setVisibility(View.GONE);
            }
        } else if (list.get(position).getOrder_status().equals("4")) {
            holder.ser_xuqiu_state.setText("已完成");
            // is_evaluation:是否评价1否2是 view_evaluation:1不显示查看评价2显示查看评价
            if (list.get(position).getIs_evaluation_mer().equals("2") &&
                    list.get(position).getView_evaluation().equals("1")) {
                holder.xuqiu_itemll.setVisibility(View.GONE);
            } else {
                holder.xuqiu_itemll.setVisibility(View.VISIBLE);
            }

            if (list.get(position).getIs_evaluation_mer().equals("1")) {
                holder.go_eva.setVisibility(View.VISIBLE);
            } else {
                holder.go_eva.setVisibility(View.GONE);
            }
            if (list.get(position).getView_evaluation().equals("1")) {
                holder.check_eva.setVisibility(View.GONE);
            } else {
                holder.check_eva.setVisibility(View.VISIBLE);
            }
        }
        holder.demand_number.setText("需求编号：" + list.get(position).getDemand_number());
        ImageLoader.getInstance().displayImage(list.get(position).getTitle_img(), holder.title_img);
        holder.item_title.setText(list.get(position).getTitle());
        holder.item_type.setText(list.get(position).getType());
        holder.offer_money.setText("报价：¥" + list.get(position).getOffer_money());
        holder.contact.setText("需求方：" + list.get(position).getContact());

//        ServiceXuqiuAdapter2 adapter2 = new ServiceXuqiuAdapter2(context);
//        holder.scrollview_listview.setAdapter(adapter2);
//        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
//                ServiceXuqiuAdapter.this.backCall.deal(R.id.scrollview_listview, position);
//            }
//        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.ser_xuqiu_itemll)
        LinearLayout ser_xuqiu_itemll;
        @ViewInject(R.id.item_ll)
        LinearLayout item_ll;
        @ViewInject(R.id.scrollview_listview)
        MyListView scrollview_listview;
        @ViewInject(R.id.ser_xuqiu_state)
        TextView ser_xuqiu_state;
        @ViewInject(R.id.check_baojia)
        TextView check_baojia;
        @ViewInject(R.id.service_finish)
        TextView service_finish;
        @ViewInject(R.id.go_eva)
        TextView go_eva;
        @ViewInject(R.id.check_eva)
        TextView check_eva;
        @ViewInject(R.id.xuqiu_itemll)
        LinearLayout xuqiu_itemll;
        @ViewInject(R.id.title_img)
        ImageView title_img;
        @ViewInject(R.id.demand_number)
        TextView demand_number;
        @ViewInject(R.id.item_title)
        TextView item_title;
        @ViewInject(R.id.item_type)
        TextView item_type;
        @ViewInject(R.id.offer_money)
        TextView offer_money;
        @ViewInject(R.id.contact)
        TextView contact;
    }
}
